﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ClientAppViaWebService.GroupsLogicServiceRef;
using ClientAppViaWebService.PersonsLogicServiceRef;
using ClientAppViaWebService.Navigators;

namespace ClientAppViaWebService.Forms
{
    public partial class GroupEditForm : EducationalDivisionEditForm
    {
        GroupsLogicService lService;
        PersonsLogicService plService;
        private String curatorId;
        private readonly Int32 ClassID = 7;

        public GroupEditForm()
        {
            InitializeComponent();
            lService = new GroupsLogicService();
            plService = new PersonsLogicService();
            classTextBox.Text = ClassID.ToString();
        }

        public String CuratorId
        {
            get { return curatorId; }
            set { curatorId = value; }
        }

        public String Curator
        {
            get { return curatorTextBox.Text; }
            set { curatorTextBox.Text = value; }
        }

        public String Shift
        {
            get { return shiftTextBox.Text; }
            set { shiftTextBox.Text = value; }
        }

        protected override void SetFormProperties(Object _object)
        {
            Group d = (_object as Group);
            ID = d.ID;
            EXT_ID = d.EXT_ID;
            ParentId = d.PARENT_ID;
            Class = d.CLASS_ID;
            ObjName = d.NAME;
            BossName = d.BOSS_ID;
            DateCreated = d.DATE_CREATED;
            Description = d.DESCRIPTION;
            Shift = d.SHIFT;
            CuratorId = d.CURATOR;
            // TODO: Set person name
            Curator = d.CURATOR_NAME;
        }

        protected override Object RetrieveFormProperties()
        {
            Group grp = new Group();
            grp.ID = ID;
            grp.EXT_ID = EXT_ID;
            grp.PARENT_ID = ParentId;
            grp.CLASS_ID = Class;
            grp.NAME = ObjName;
            grp.DataBaseName = DataBaseName;
            grp.BOSS_ID = BossName;
            grp.DATE_CREATED = DateCreated;
            grp.DESCRIPTION = Description;
            grp.SHIFT = Shift;
            grp.CURATOR = CuratorId;
            return grp;
        }

        protected override void LoadEditFormData(int parentNodeID)
        {
            Group p = lService.SelectGroup_By_DataBase_And_Id(DataBaseName, parentNodeID);
            SetFormProperties(p);
        }

        protected override void UpdateEntity()
        {
            Group obj = RetrieveFormProperties() as Group;
            lService.UpdateGroup(obj);
        }

        protected override void InsertEntity()
        {
            Group obj = RetrieveFormProperties() as Group;
            lService.InsertGroup(obj);
        }

        private void curatorButton_Click(object sender, EventArgs e)
        {
            ObjectSelectionNavigatorForm f = new ObjectSelectionNavigatorForm();
            f.LoadSearchingNavigator(CuratorId, new List<int>(new int[] { 20 }));
            if (f.ShowDialog() == DialogResult.OK)
            {
                CuratorId = f.SelectedObjectExtId;
                Curator = f.SelectedObjectName;
            }
        }
    }
}
